package com.chainshop.returns.service.impl;

import java.util.List;
import com.chainshop.common.core.domain.pojo.ItemInformation;
import com.chainshop.common.core.domain.pojo.ProductReturns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.chainshop.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.chainshop.returns.mapper.ProductReturnsMapper;
import com.chainshop.returns.service.IProductReturnsService;

/**
 * 店铺退货记录表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-15
 */
@Service
public class ProductReturnsServiceImpl implements IProductReturnsService {
    @Autowired
    private ProductReturnsMapper productReturnsMapper;

    /**
     * 查询店铺退货记录表
     *
     * @param id 店铺退货记录表主键
     * @return 店铺退货记录表
     */
    @Override
    public ProductReturns selectProductReturnsById(Integer id) {
        return productReturnsMapper.selectProductReturnsById(id);
    }

    /**
     * 查询店铺退货记录表列表
     *
     * @param productReturns 店铺退货记录表
     * @return 店铺退货记录表
     */
    @Override
    public List<ProductReturns> selectProductReturnsList(ProductReturns productReturns) {
        return productReturnsMapper.selectProductReturnsList(productReturns);
    }

    /**
     * 新增店铺退货记录表
     *
     * @param productReturns 店铺退货记录表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductReturns(ProductReturns productReturns) {
        int rows = productReturnsMapper.insertProductReturns(productReturns);
        insertItemInformation(productReturns);
        return rows;
    }

    /**
     * 修改店铺退货记录表
     *
     * @param productReturns 店铺退货记录表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductReturns(ProductReturns productReturns) {
        productReturnsMapper.deleteItemInformationById(productReturns.getId());
        insertItemInformation(productReturns);
        return productReturnsMapper.updateProductReturns(productReturns);
    }

    /**
     * 批量删除店铺退货记录表
     *
     * @param ids 需要删除的店铺退货记录表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductReturnsByIds(Integer[] ids) {
        productReturnsMapper.deleteItemInformationByIds(ids);
        return productReturnsMapper.deleteProductReturnsByIds(ids);
    }

    /**
     * 删除店铺退货记录表信息
     *
     * @param id 店铺退货记录表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductReturnsById(Integer id) {
        productReturnsMapper.deleteItemInformationById(id);
        return productReturnsMapper.deleteProductReturnsById(id);
    }

    /**
     * 新增存储商品详细信息的信息
     *
     * @param productReturns 店铺退货记录表对象
     */
    public void insertItemInformation(ProductReturns productReturns) {
       List<ItemInformation> itemInformationList = productReturns.getItemInformationList();
        Long id =(long) productReturns.getId();
        if (StringUtils.isNotNull(itemInformationList)) {
            List<ItemInformation> list = new ArrayList<ItemInformation>();
            for (ItemInformation itemInformation : itemInformationList) {
                itemInformation.setId(id);
                list.add(itemInformation);
            }
            if (list.size() > 0) {
                productReturnsMapper.batchItemInformation(list);
            }
        }
    }
}
